//
//  CXHTopOnInterstitialAdapter.m
//  CXHAdSDK-TopOn
//
//  Created by 陈坤 on 2024/10/11.
//

#import "CXHTopOnInterstitialAdapter.h"
#import "CXHTopOnInterstitialEvent.h"
#import "CXHTopOnCommonBiddingManager.h"
#import "CXHTopOnCommonBiddingRequest.h"
#if __has_include(<AnyThinkSDK/AnyThinkSDK.h>)
#import <AnyThinkSDK/AnyThinkSDK.h>
#else
#import "AnyThinkSDK.h"
#endif
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Core/CXHAdSDKInterstitialAd.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHTopOnInterstitialAdapter ()<CXHAdSDKInterstitialAdDelegate>

@property (nonatomic, strong) CXHTopOnInterstitialEvent *customEvent;

@property (nonatomic, strong) CXHAdSDKInterstitialAd *interstitialAd;

@property (nonatomic, assign) BOOL isC2SBinding;

@end

@implementation CXHTopOnInterstitialAdapter

#pragma mark - Ad load
- (void)loadADWithArgument:(ATAdMediationArgument *)argument {
    self.interstitialAd = [[CXHAdSDKInterstitialAd alloc] init];
    self.interstitialAd.delegate = self;
    self.interstitialAd.posId = argument.serverContentDic[@"posid"];
    [self.interstitialAd loadAdData];
    
}
 
#pragma mark - Ad show
- (void)showInterstitialInViewController:(UIViewController *)viewController {
    self.interstitialAd.controller = viewController;
    [self.interstitialAd show];
}
 
#pragma mark - Ad ready
- (BOOL)adReadyInterstitialWithInfo:(NSDictionary *)info {
    return self.interstitialAd.isAdValid;
}

#pragma mark - C2S Win Loss
- (void)didReceiveBidResult:(ATBidWinLossResult *)result {
    if (result.bidResultType == ATBidWinLossResultTypeWin) {
        [self.interstitialAd sendWinNotice:[result.winPrice integerValue]];
        return;
    }
    [self.interstitialAd sendLossNotice:[result.secondPrice integerValue]];
}

#pragma mark - CXHAdSDKInterstitialAdDelegate

/**
 CXHAdSDKIntertitialAd请求成功回调
 
 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdSuccedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd{
    NSDictionary * extraDic = [CXHTopOnCommonAdapter getC2SInfo:[self.interstitialAd getBidPrice]];
    [self.adStatusBridge atOnInterstitialAdLoadedExtra:extraDic];
}

/**
 CXHAdSDKIntertitialAd请求失败回调

 @param interstitialAd 插屏广告实例对象
 @param error 失败原因
*/
- (void)CXH_interstitialAdFailedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    [self.adStatusBridge atOnAdLoadFailed:error adExtra:nil];
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidPresent:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内失败回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdFailedToPresent:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    [self.adStatusBridge atOnAdShowFailed:error extra:nil];
}

/**
 CXHAdSDKIntertitialAd点击回调

 @param interstitialAd 插屏广告实例对象
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
*/
- (void)CXH_interstitialAdDidClick:(CXHAdSDKInterstitialAd *)interstitialAd loadingPageURL:(NSString *)loadingPageURL{
    [self.adStatusBridge atOnAdClick:nil];
}

/**
 CXHAdSDKIntertitialAd关闭回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClose:(CXHAdSDKInterstitialAd *)interstitialAd{
    [self.adStatusBridge atOnAdClosed:nil];
}

/**
 CXHAdSDKIntertitialAd展示回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposure:(CXHAdSDKInterstitialAd *)interstitialAd{
    [self.adStatusBridge atOnAdShow:nil];
}

/**
 CXHAdSDKIntertitialAd展示上报回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposureReport:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}


/**
 CXHAdSDKIntertitialAd点击上报回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClickReport:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd关闭落地页回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdCloseLandingPage:(CXHAdSDKInterstitialAd *)interstitialAd{
    [self.adStatusBridge atOnAdDetailClosed:nil];
}


//- (nonnull instancetype)initWithNetworkCustomInfo:(nonnull NSDictionary *)serverInfo localInfo:(nonnull NSDictionary *)localInfo {
//    self = [super init];
//    if (self != nil) {
//        NSString *domain = [serverInfo CXH_objectOrNilForKey:@"domain"];
//        NSString *appid = [serverInfo CXH_objectOrNilForKey:@"appid"];
//        if (!kCXHStringIsEmpty(appid)) {
//            [CXHAdSDK initWithAppId:appid requestDomain:domain completionBlock:^(NSError * _Nullable error) {
//                if (error) {
//                    NSLog(@"CXHSDK 初始化失败%@",error);
//                }
//            }];
//        }
//    }
//    return self;
//}
//
//- (void)loadADWithInfo:(nonnull NSDictionary *)serverInfo localInfo:(nonnull NSDictionary *)localInfo completion:(nonnull void (^)(NSArray<NSDictionary *> * _Nonnull, NSError * _Nonnull))completion {
//    NSString *posid = serverInfo[@"posid"];
//    NSString *requestUUID = serverInfo[@"tracking_info_request_id"];
//    
//    NSString *bidId = serverInfo[kATAdapterCustomInfoBuyeruIdKey];
//    if (bidId) {
//        CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingManager sharedInstance] getRequestItemWithUnitID:requestUUID];
//        
//        if (request) {
//            self.customEvent = (CXHTopOnInterstitialEvent *)request.customEvent;
//            self.customEvent.requestCompletionBlock = completion;
//            self.interstitialAd = request.customObject;
//            [self.customEvent trackInterstitialAdLoaded:self.interstitialAd adExtra:nil];
//        }
//        [[CXHTopOnCommonBiddingManager sharedInstance] removeRequestItemWithUnitID:requestUUID];
//        return;
//    }
////    NSString *requestUUID = serverInfo[@"tracking_info_request_id"];
//    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        NSLog(@"======普通加载广告====");
//        self.customEvent = [[CXHTopOnInterstitialEvent alloc] initWithInfo:serverInfo localInfo:localInfo];
//        self.customEvent.networkAdvertisingID = posid;
//        self.customEvent.requestCompletionBlock = completion;
//
//        self.interstitialAd = [[CXHAdSDKInterstitialAd alloc] init];
//        self.interstitialAd.delegate = self.customEvent;
//        self.interstitialAd.posId = posid;
//        [self.interstitialAd loadAdData];
//    });
//}
//
//+ (BOOL)adReadyWithCustomObject:(nonnull id)customObject info:(nonnull NSDictionary *)info {
//    CXHAdSDKInterstitialAd *interstitialAd = (CXHAdSDKInterstitialAd *)customObject;
//    return [interstitialAd isAdValid];
//}
//
//+ (BOOL)isSupportAdType:(nonnull ATUnitGroupModel *)unitGroupModel { 
//    return YES;
//}
//
//
//+ (void) showInterstitial:(ATInterstitial*)interstitial inViewController:(UIViewController*)viewController delegate:(id)delegate {
//    CXHAdSDKInterstitialAd *interstitialAd = interstitial.customObject;
//    interstitial.customEvent.delegate = delegate;
//    interstitialAd.controller = viewController;
//    [interstitialAd show];
//}
//
//#pragma mark - AlexC2SBiddingRequestProtocol
//+ (void)bidRequestWithPlacementModel:(nonnull ATPlacementModel *)placementModel
//                      unitGroupModel:(nonnull ATUnitGroupModel *)unitGroupModel
//                                info:(nonnull NSDictionary *)info
//                          completion:(nonnull void (^)(ATBidInfo * _Nonnull, NSError * _Nonnull))completion {
//    
//    NSLog(@"------> menta start bidding");
//    NSString *domain = [info CXH_objectOrNilForKey:@"domain"];
//    NSString *appid = [info CXH_objectOrNilForKey:@"appid"];
//    NSString *posid = info[@"posid"];
//    [CXHTopOnInterstitialAdapter initCXHSDKWith:appid domain:domain completion:^{}];
//    
//    
//    NSString *requestUUID = info[@"tracking_info_request_id"];
//    NSLog(@"=====requestuuid%@", requestUUID);
//    CXHTopOnInterstitialEvent *customEvent = [[CXHTopOnInterstitialEvent alloc]initWithInfo:info localInfo:info];
//    customEvent.isC2SBiding = YES;
//    customEvent.UUID = requestUUID;
//    
//    CXHTopOnCommonBiddingRequest * req = [CXHTopOnCommonBiddingRequest new];
//    req.customEvent = customEvent;
//    req.unitGroup = unitGroupModel;
//    req.placementID = placementModel.placementID;
//    req.bidCompletion = completion;
//    req.unitID = posid;
//    req.extraInfo = info;
//    req.adType = CXHAdFormatInterstitial;
//    req.UUID = requestUUID;
//    
//    [[CXHTopOnCommonBiddingManager sharedInstance] startWithRequestItem:req];
//    NSLog(@"------> menta start inter bidding %@", requestUUID);
//}
//
////// 返回广告位比价胜利时，第二的价格的回调，可在该回调中向三方平台返回竞胜价格  secondPrice：美元(USD)
//+ (void) sendWinnerNotifyWithCustomObject:(id)customObject secondPrice:(NSString*)price userInfo:(NSDictionary<NSString *, NSString *> *)userInfo {
//    NSLog(@"------> menta interstitial ad win");
//    CXHAdSDKInterstitialAd *interstitialAd = (CXHAdSDKInterstitialAd *)customObject;
//    [interstitialAd sendWinNotice:[price integerValue]];
//}
//
////// 返回广告位比价输了的回调，可在该回调中向三方平台返回竞败价格 winPrice：美元(USD)
//+ (void)sendLossNotifyWithCustomObject:(nonnull id)customObject lossType:(ATBiddingLossType)lossType winPrice:(nonnull NSString *)price userInfo:(NSDictionary *)userInfo {
//    CXHAdSDKInterstitialAd *interstitialAd = (CXHAdSDKInterstitialAd *)customObject;
//    [interstitialAd sendLossNotice:[price integerValue]];
//}
//
//+ (void)initCXHSDKWith:(NSString*)appid domain:(NSString *)domain completion:(void (^)(void))completion {
//    NSLog(@"====== start init cxh sdk");
//    if (kCXHStringIsEmpty(appid)) {
//        return;
//    }
//    [CXHAdSDK initWithAppId:appid requestDomain:domain completionBlock:^(NSError * _Nullable error) {
//        if (!error && completion != nil) {
//            completion();
//        }
//    }];
//}

- (void)dealloc
{
    NSLog(@"------> %s", __FUNCTION__);
}

@end
